//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "CDBChkb.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
#define SPC_LEFT_RIGHT  18
#define SPC_LEFT_LEFT    2
//---------------------------------------------------------------------------
// ValidCtrCheck est utilisé pour être sûr que les composants créés ne
// contiennent aucune fonction virtuelle pure.
//

static inline void ValidCtrCheck(CDBCheckBox *)
{
    new CDBCheckBox(NULL);
}
//---------------------------------------------------------------------------
__fastcall CDBCheckBox::CDBCheckBox(TComponent* Owner)
    : TDBCheckBox(Owner)
{
  m_ReadOnly     = false;
  m_Enabled      = true;
}
//---------------------------------------------------------------------------
void __fastcall CDBCheckBox::SetEnabled(bool value)
{
  m_Enabled =  value;
  TDBCheckBox::Enabled = ( m_Enabled && !m_ReadOnly);
  Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall CDBCheckBox::SetReadOnly(bool value)
{
  m_ReadOnly            =  value;
  TDBCheckBox::ReadOnly =  value;
  TDBCheckBox::Enabled = ( m_Enabled && !m_ReadOnly);
  Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall CDBCheckBox::WMPaint(TWMPaint &Msg)
{
  TDBCheckBox::Dispatch(&Msg);

  HDC        hdc;
  HFONT      SaveFont;
  AnsiString szCaption;
  int        p, posx = (Alignment==taRightJustify ? SPC_LEFT_RIGHT : SPC_LEFT_LEFT);

  szCaption = Caption;
  if ((p=Caption.Pos("&")) != 0) szCaption.Delete(p, 1);

  hdc      = ::GetDC(Handle);
  SaveFont = ::SelectObject(hdc, Font->Handle);
  if (!Enabled) {
    ::SetBkMode(hdc, TRANSPARENT);
    ::SetTextColor(hdc, ::GetSysColor(COLOR_BTNHIGHLIGHT));
    ::TextOut(hdc, posx + 1, (Height / 2) - 7 + 1, szCaption.c_str(), szCaption.Length());
    ::SetTextColor(hdc, ::GetSysColor(COLOR_BTNSHADOW   ));
    ::TextOut(hdc, posx    , (Height / 2) - 7    , szCaption.c_str(), szCaption.Length());
  } else {
    if ( Parent) {
      class CControl : public TControl {
        public :
          __fastcall CControl( TComponent *pOwner);
          __property Color;
      };
      ::SetBkColor( hdc, ColorToRGB((( CControl*)Parent)->Color));
      ::SetBkMode ( hdc, OPAQUE);
    }
    ::SetTextColor(hdc, Font->Color);
    ::TextOut(hdc, posx    , (Height / 2) - 7    , szCaption.c_str(), szCaption.Length());
  }
  ::SelectObject(hdc, SaveFont);
  ::ReleaseDC(Handle,hdc);
}
//---------------------------------------------------------------------------
namespace Cdbchkb
{
    void __fastcall PACKAGE Register()
    {
        TComponentClass classes[1] = {__classid(CDBCheckBox)};
        RegisterComponents("EIC CtrlDB", classes, 0);
    }
}
//---------------------------------------------------------------------------
